{layout name="layouts/main" /}

<?php

use app\admin\model\field\system\Account;

$tableClass = new \app\admin\extend\common\html2\Table();
echo $tableClass->addTitle($title);
echo $search ?? '';
echo $tableClass->table()->getTable();
$btn = [
    ['auth','编辑权限'],
	['update','编辑'],
    ['resetPassword', '重置密码'],
	['del','删除'],
];
echo $tableClass->jsBtn('barDemo',$btn);
echo $tableClass->toolCheckbox('toolStatus', 'status', 'id', '启用|禁用');


?>


<script>
	//表格
	layui.use(['table'], function() {
		const table = layui.table;
		const form  = layui.form;
		const $ = layui.$;
		const selfLayui = new SelfLayui(layui,$);

		//渲染
		const cols = [[
			{field:'id', title:'<?php echo Account::id ?>' },
            {field:'username', title:'<?php echo Account::username ?>' },
            {field:'phone', title:'<?php echo Account::phone ?>' },
            {field:'lastlogin', title:'<?php echo Account::lastLogin ?>' },
            {field:'status', title:'<?php echo Account::status ?>', toolbar: '#toolStatus',},

            {fixed: 'right', title:'操作', width:300, toolbar: '#barDemo'}
		]];
		const url = './getData';

		tableRender(cols,url);

		//搜索
		const active = {
			reload: function(){
				//执行重载
				const key = selfLayui.getSearch()
				selfLayui.reload(key);
			}
			,del: function(){
				//通过复选框删除
				const url = './batchDel';
				selfLayui.delBatch(url);
			}
			,add:function() {
				//添加
				selfLayui.openAdd('添加<?php echo $title;?>','./add');
			}
		};
		$('.demoTable .layui-btn').on('click', function(){
			const type = $(this).data('type');
			active[type] ? active[type].call(this) : '';
		});

		//监听状态开关
		form.on('switch(toolStatus)', function(data){
			const url = './status';
			selfLayui.changeStatus(url,data);
		});

		//监听工具条
		table.on('tool(test)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
			const data = obj.data; //获得当前行数据
			const layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                        
			if(layEvent === 'del'){
				//删除
				const url = './del?id='+obj.data.id;
				selfLayui.del(url);
			} else if(layEvent === 'update'){
				//编辑
				selfLayui.openAdd('编辑<?php echo $title;?>','./update?id='+data.id);
			} else if(layEvent === 'resetPassword') {
                // 重置密码
                const url = './resetPassword?id=' + data.id
                const msg = '确实重试密码吗?'
                const reload = true
                const call = function(password) {
                    layer.confirm('新密码: [' + password + '], 请惠存', function(index) {
                        layer.close(index)
                    })
                }
                selfLayui.getUrl(url, msg, reload, null, call)
            }else if(layEvent==='auth'){
                //编辑
				selfLayui.openAdd('权限编辑<?php echo $title;?>','./auth?id='+data.id);
            }
		});
	});
</script>

